{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def detect_cycle(head):\n",
    "    if not head:\n",
    "        return\n",
    "    \n",
    "    # cycle detection: 2k = k (mod C)\n",
    "    p, q = head, head.next\n",
    "    while q and p is not q:\n",
    "        p, q = p.next, q.next and q.next.next\n",
    "\n",
    "    if p is q:\n",
    "        # cycle removal: k + T = 0 (mod C)\n",
    "        p = head\n",
    "        while p is not q.next:\n",
    "            p, q = p.next, q.next\n",
    "        \n",
    "        # fix the last link\n",
    "        q.next = None\n",
    "        return q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## utilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Node:\n",
    "    def __init__(self, **kwargs):\n",
    "        self.__dict__ = kwargs\n",
    "        \n",
    "    def __repr__(self):\n",
    "        return str(self.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def linked_list_with_cycle(length, cycle):\n",
    "    head, tail = None, None\n",
    "    \n",
    "    for i in range(length):\n",
    "        # prepend head\n",
    "        head = Node(value=length - i, next=head)\n",
    "        tail = tail or head\n",
    "        \n",
    "        # make cycle of length C\n",
    "        if i + 1 == cycle:\n",
    "            tail.next = head\n",
    "        \n",
    "    return head"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'value': 1, 'next': {'value': 2, 'next': {'value': 3, 'next': {'value': 4, 'next': {'value': 5, 'next': {'value': 6, 'next': {'value': 7, 'next': {'value': 8, 'next': {'value': 9, 'next': {'value': 10, 'next': {...}}}}}}}}}}}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'value': 10, 'next': None}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linked_list = linked_list_with_cycle(10, 4)\n",
    "print(linked_list)\n",
    "detect_cycle(linked_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'value': 5, 'next': None}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linked_list = linked_list_with_cycle(5, 1)\n",
    "detect_cycle(linked_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'value': 10, 'next': None}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linked_list = linked_list_with_cycle(10, 5)\n",
    "detect_cycle(linked_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'value': 25, 'next': None}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linked_list = linked_list_with_cycle(25, 25)\n",
    "detect_cycle(linked_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
